<?php
namespace app\ask\controller;

use think\Controller;

class Checkin extends Controller
{
    //关联查询的意思
    protected $relationSearch = true;
    public function __construct()
    {
        parent::__construct();
        $this->BusinessModel=model("Business.Business");
        $this->CheckinModel=model("Post.Checkin");
        $this->PayModel=model("Post.Pay");
    }

    // 查询签到数据
    public function index()
    {
        if($this->request->isPost())
        {
            $busid=$this->request->param('busid',0,'trim');
            $business=$this->BusinessModel->find($busid);
            if(!$business)
            {
                $this->error('用户不存在,非法登录');
                exit;
            }
            $result=$this->CheckinModel->where(['busid'=>$busid])->select();
            if(!$result)
            {
                $this->error("暂无签到记录",null,$result);
                exit;
            }
            $this->success('返回签到数据',null,$result);
            exit;
        }
    }

    // 签到
    public function add()
    {
        if($this->request->isPost())
        {
            $params=$this->request->param('');
            $business=$this->BusinessModel->find($params['busid']);
            if(!$business)
            {
                $this->error('用户不存在,非法登录');
                exit;
            }

            // 签到的逻辑
            // 签到表 插入
            // 用户余额 更新
            // 获得签到积分 充值记录表 插入
            

            // 加载模型
            $CheckinModel=model("Post.Checkin");
            $BusinessModel=model("Business.Business");
            $PayModel=model("Post.Pay");
            
            // 开启事务
            $CheckinModel->startTrans();
            $BusinessModel->startTrans();
            $PayModel->startTrans();

            // 插入签到表
            $CheckinStatus=$this->CheckinModel->validate("common/Post/Checkin")->save($params);
            if($CheckinStatus===FALSE)
            {
                $this->error($this->CheckinModel->getError());
                exit;
            }
            $money=$business['money'];
            $score=$params['score'];
            $update=bcadd($money,$score);
            // 余额更新
            $BusinessData=[
                'id'=>$business['id'],
                'money'=>$update,
            ];
            //验证器
            $validate = [
                [
                    'money' => ['number', '>=:0'],
                ],
                [
                    'money.number'      => '余额必须是数字类型',
                    'money.>='      => '余额必须大于等于0元',
                ]
            ];
            $BusinessStatus=$BusinessModel->validate(...$validate)->isUpdate(true)->save($BusinessData);
            if($BusinessStatus===FALSE)
            {
                $CheckinModel->rollback();
                $this->error($BusinessModel->getError());
                exit;
            }
            // 充值记录表插入数据
            $content = "签到成功获得 ￥$score 积分";
            $PayData = [
                'total' => $score,
                'busid' => $business['id'],
                'content' => $content
            ];
            $PayStatus=$PayModel->validate('common/Post/Pay')->save($PayData);
            if($PayStatus===FALSE)
            {
                $BusinessModel->rollback();
                $CheckinModel->rollback();
                $this->error($PayModel->getError());
                exit;
            }
            if($CheckinStatus ===FALSE || $BusinessStatus === FALSE || $PayStatus === FALSE)
            {
                $PayModel->rollback();
                $BusinessModel->rollback();
                $CheckinModel->rollback();
                $this->error('签到失败');
                exit;
            }else
            {
                $CheckinModel->commit();
                $BusinessModel->commit();
                $PayModel->commit();
                $this->success('签到成功');
                exit;
            }
        }
    }
 
}